import { GameManager } from "./GameManager";
import { HookCollider } from "./HookCollider";
import { Singleton } from "./Singleton";

export class ShopItemManager<T> extends Singleton<T> {

    public bombCount; //炸弹数
    public stoneValueUpCount = 0; //石头价值提升数
    public isStrengthUp: boolean = false; //是否强壮
    public buyItemIndex: number = 0;
    public tipLabel: cc.Label;

    public itemDescText: string[] = [
        '当你拉起比较重的物品时，可以炸毁以节省时间(100金币)',
        '接下来的3个石头变得更有价值，价格变成3倍(50金币)',
        '变得强壮，拉重物不费力,持续一回合(100金币)'
    ]
    maxBombCount: number = 4; //最大炸弹鼠
    bombsSheet: cc.Node; //炸弹架

    constructor() {
        super();
        this.bombsSheet = cc.find("bombsSheet");
        this.tipLabel = cc.find("shop-panel/shop-desk/tipLabel").getComponent(cc.Label);
    }

    /**
     * 增加炸弹数
     */
    addBombCount() {
        if (this.bombCount < this.maxBombCount) {
            if (GameManager.Instance(GameManager).money > 100) {
                GameManager.Instance(GameManager).money -= 100;
                this.bombCount++;
                cc.sys.localStorage.setItem('bombCount', this.bombCount)
                this.refreshDisplay();
            }
        }
    }

    /**
     * 减少炸弹数
     */
    reduceBombCount() {
        if (this.bombCount >= 0) {
            this.bombCount--;
            this.refreshDisplay();
        }
    }

    /**
     * 刷新炸弹架子显示
     */
    refreshDisplay() {
        this.bombsSheet.children.forEach(function (v) {
            v.active = false;
        })
        for (let i = 0; i < this.bombCount; i++) {
            this.bombsSheet.children[i].active = true;
        }
    }

    /**
     * 购买物品
     */
    buyItem() {
        switch (Number(this.buyItemIndex)) {
            case 0:
                ShopItemManager.Instance(ShopItemManager).addBombCount();
                break;
            case 1:
                //购买石头提升价值次数
                ShopItemManager.Instance(ShopItemManager).stoneValueUpCount = Number(ShopItemManager.Instance(ShopItemManager).stoneValueUpCount) + 3;
                cc.log(ShopItemManager.Instance(ShopItemManager).stoneValueUpCount)
                cc.sys.localStorage.setItem('stoneValueUpCount', ShopItemManager.Instance(ShopItemManager).stoneValueUpCount);
                GameManager.Instance(GameManager).money -= 50;
                break;
            case 2:
                // 购买强壮药剂
                ShopItemManager.Instance(ShopItemManager).isStrengthUp = true;
                GameManager.Instance(GameManager).money -= 100;
                break;
        }

    }

}
